package com.darsystems.foghourz.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class FBLogout implements EntryPoint {

	@Override
	public void onModuleLoad() {
		Cookies.removeCookie(FBAuthorizer.TOKEN_COOKIE_NAME);
		String token = Window.Location.getParameter("token");

		if(token != null && token.length() > 0)
		{
			FogBugzServiceAsync fogbugzService = GWT
				.create(FogBugzService.class);
			
			fogbugzService.executeCommand(token, "logout", "", new AsyncCallback<FBResponse>(){
				public void onFailure(Throwable caught) {
				}
				public void onSuccess(FBResponse result) {
					gotoMainPage();
				}
			});
		}
		else
		{
			gotoMainPage();
		}
	}

	private void gotoMainPage() {
		Window.Location.replace(GWT.getHostPageBaseURL());
	}

}
